<?php
/**
 *
 */
namespace app\common\query;

use app\common\model\MemberAddress as MemberAddressModel;

class MemberAddress
{

    /**
     * 查询用户的所有地址
     * @param  [type] $mid [description]
     * @return [type]      [description]
     */
	public function memberAddress($mid,$field=''){
        $model = new MemberAddressModel;
        $data = $model->where(['mid'=>$mid])->field($field)->order('id desc')->select()->toArray();
        // foreach($data as &$row):
        //         $row = self::setRemark($row);
        // endforeach;
        return $data;
    }

    /**
     * 查询用户单个地址
     * @return [type] [description]
     */
    public function memberAddressFind($aid=''){
        $model = new MemberAddressModel;
        $res = $model -> where(['id'=>$aid])->field('id,city,uname,mphone,address,default,title')->find()-> toArray();
         return    self::setRemark($res);
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setRemark($row){

        if(isset($row['city'])){
            $query = new \app\common\query\City;

            // 省
            $address_province = $query->findName(substr($row['city'], 0,strlen($row['city'])-4));
            // 市
            $address_city= $query->findName((substr($row['city'], 0,strlen($row['city'])-2)));
            // 县
            $address_county = $query->findName($row['city']);
            $row['city_name'] = $address_province.$address_city.$address_county;
        }
        return $row;
    }



}